package com.moive.news.tqnewsapplication.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.adapter.SearchAdapter;
import com.moive.news.tqnewsapplication.base.BaseFragment;
import com.moive.news.tqnewsapplication.entity.SearchInfo;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.GetSystemTimeUitl;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.mylistview.SecondXListView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/11.
 */

public class FansFragment extends BaseFragment implements SecondXListView.IXListViewListener {
    @BindView(R.id.pub_all_listview)
    SecondXListView pubAllListview;
    @BindView(R.id.pub_all_myText)
    TextView pubAllMyText;
    Unbinder unbinder;
    private Gson gson = new Gson();
    private int page = 1;
    private static final String TAG = "FansFragment";
    SearchAdapter ad;
    List<SearchInfo.DataBean> list = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.xlistview, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }
    private void init() {
        ad = new SearchAdapter(getActivity());
        pubAllListview.setAdapter(ad);
        pubAllListview.setXListViewListener(this);
        pubAllListview.setPullLoadEnable(true);
        pubAllListview.setPullRefreshEnable(true);
        getdata();
    }
    private void getdata() {
        OkHttpUtils.post()
                .url(BaseApi.FAN_LIST)
                .addParams("userID", PreferenceUtil.getString("id", ""))
                .addParams("page", page + "")
                .addParams("rows", "10")
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }
            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                                SearchInfo.DataBean dataBean = gson.fromJson(jsonObject1 + "", SearchInfo.DataBean.class);
                                dataBean.setGuanzhu(false);
                                list.add(dataBean);
                            }
                            ad.addItems(list);
                        }
                        if (jsonArray.length()==0){
                            ToastUtils.ShortToast("没有更多数据了！");
                        }
                    }

                } catch (JSONException e) {
                }
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Override
    public void onRefresh() {
        page = 1;
        list.clear();
        getdata();
        ad.notifyDataSetChanged();
        stopOnLoad();
    }
    private void stopOnLoad() {
        pubAllListview.stopRefresh();
        pubAllListview.stopLoadMore();
        pubAllListview.setRefreshTime(GetSystemTimeUitl.getTime());
    }
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                page++;
                getdata();
                ad.notifyDataSetChanged();
                stopOnLoad();
            }
        }, 1000);
    }
}
